package de.in.tum.msspp.Logger;

import java.util.List;
import de.in.tum.msspp.R;
import android.app.Activity;
import android.hardware.SensorManager;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class DataLogger extends Activity{
	
	boolean isLogging;
	String activity;
	Button button;
	TextView clock;
	CountDownTimer countdown;
	Accelerometer accelerometer;
	Compass compass;
	CSVExport export;
	LocationRequestor location;

	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        
        button = (Button) findViewById(R.id.logButton);
        clock = (TextView) findViewById(R.id.clock);    

        SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); //Get general SensorManager`
        accelerometer = new Accelerometer(sensorManager);
        compass = new Compass(sensorManager, accelerometer);
        LocationManager locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
        location = new LocationRequestor(locationManager);
        
        countdown = new CountDownTimer(1800000, 1000) {

            /** Called each time the countdown ticks as long as the countdown ticks. 
             * Collects the data and appends it to our StringBuilder for export.
             *  
             */
            public void onTick(long millisUntilFinished) {

            	StringBuilder strB = new StringBuilder();
            	
            	clock.setText("seconds remaining: " + millisUntilFinished / 1000);          	
            	
            	Log.i("ACCELEROMETER", Double.toString(accelerometer.getAcceleration()));
            	Log.i("COMPASS", Double.toString(compass.getDirection()));
            		
            	if(location.getLocation() != null){
            		strB.append(Double.toString(location.getLocation().getLatitude())+",");
            		Log.i("LATITUDE", Double.toString(location.getLocation().getLatitude()));
            		strB.append(Double.toString(location.getLocation().getLongitude())+",");
            		Log.i("LONGITUDE", Double.toString(location.getLocation().getLongitude()));
            		
            	}
            	else{
            		strB.append(" ,");
            		Log.i("LATITUDE", "No Latitude found!");
            		strB.append(" ,");
            		Log.i("LONGITUDE", "No longitude found!");
            	}
            	
            	strB.append(Double.toString(accelerometer.getAcceleration())+",");
            	strB.append(Double.toString(compass.getDirection())+",");
            	strB.append(Long.toString(System.currentTimeMillis())+"\n");
            	export.appendToOutputString(strB.toString());
            	
            	
            }

            public void onFinish() {
                clock.setText("done!");
                List<String[]> data = export.getDataLines();
         		export.writeAllToFile(data);
            }
        };

       
    }

    public void buttonClicked(View view){
    	if(!isLogging){
    		isLogging = true;
    		EditText input = (EditText) findViewById(R.id.context);
    		activity = input.getText().toString();
        	button.setText(R.string.stop);
        	
        	 try {
     			export = new CSVExport(activity);
     		} catch (Exception e) {
     			Log.i("DataLogger", e.getMessage());
     			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
     		}
     		
        	countdown.start();
        }
    	else{
    		isLogging = false;
    		button.setText(R.string.start);
    		countdown.cancel();
    	}
    }
    
    @Override
    public void onStart(){
    	super.onStart();
    	location.startListening();
    }
    
    @Override
    public void onStop(){
    	location.stopListening();
    	super.onStop();
    }
}